package com.bsg.upm.check;

import java.text.MessageFormat;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpStatus;
import org.springframework.stereotype.Service;

import com.bsg.upm.constant.Messages;
import com.bsg.upm.entity.DefinitionSubServSoftwareEntity;
import com.bsg.upm.entity.SiteEntity;
import com.bsg.upm.entity.SubServSoftwareImageEntity;

/**
 * 软件镜像业务处理检查
 * 
 * @author HCK
 *
 */
@Service
public class SoftwareImageCheck extends BaseCheck {

	/**
	 * 软件镜像新增时检查
	 * 
	 * @param paramMap
	 *            请求参数
	 * @return CheckResult检查结果对象
	 */
	public CheckResult checkSave(Map<String, Object> paramMap) {
		// 非逻辑检查
		CheckResult chkRS = checkSaveNonLogic(paramMap);
		if (chkRS.isError()) {
			return chkRS;
		}

		// 逻辑检查
		chkRS = checkSaveLogic(paramMap);
		if (chkRS.isError()) {
			return chkRS;
		}
		return chkRS;
	}

	/**
	 * 软件镜像启用时检查
	 * 
	 * @param subServSoftwareImage
	 *            软件镜像对象
	 * @return CheckResult检查结果对象
	 */
	public CheckResult checkEnable(SubServSoftwareImageEntity subServSoftwareImage) {
		CheckResult chkRS = new CheckResult();
		// 存在性检查
		if (subServSoftwareImage == null) {
			chkRS.setStatus(HttpStatus.SC_GONE);
			chkRS.setErrorMsg(MessageFormat.format(Messages.NOT_EXIST, "软件镜像"));
			return chkRS;
		}

		// 软件镜像状态检查
		if (subServSoftwareImage.isEnabled()) {
			chkRS.setStatus(HttpStatus.SC_CONFLICT);
			chkRS.setErrorMsg(Messages.ENABLED_ALREADY);
			return chkRS;
		}
		return chkRS;
	}

	/**
	 * 软件镜像停用时检查
	 * 
	 * @param subServSoftwareImage
	 *            软件镜像对象
	 * @return CheckResult检查结果对象
	 */
	public CheckResult checkDisable(SubServSoftwareImageEntity subServSoftwareImage) {
		CheckResult chkRS = new CheckResult();
		// 存在性检查
		if (subServSoftwareImage == null) {
			chkRS.setStatus(HttpStatus.SC_GONE);
			chkRS.setErrorMsg(MessageFormat.format(Messages.NOT_EXIST, "软件镜像"));
			return chkRS;
		}

		// 软件镜像状态检查
		if (!subServSoftwareImage.isEnabled()) {
			chkRS.setStatus(HttpStatus.SC_GONE);
			chkRS.setErrorMsg(Messages.DISABLED_ALREADY);
			return chkRS;
		}
		return chkRS;
	}

	/**
	 * 软件镜像删除时检查
	 * 
	 * @param subServSoftwareImage
	 *            软件镜像对象
	 * @return CheckResult检查结果对象
	 */
	public CheckResult checkRemove(SubServSoftwareImageEntity subServSoftwareImage) {
		CheckResult chkRS = new CheckResult();
		// 存在性检查
		if (subServSoftwareImage == null) {
			chkRS.setStatus(HttpStatus.SC_GONE);
			chkRS.setErrorMsg(MessageFormat.format(Messages.NOT_EXIST, "软件镜像"));
			return chkRS;
		}

		// 软件镜像状态检查
		if (subServSoftwareImage.isEnabled()) {
			chkRS.setStatus(HttpStatus.SC_CONFLICT);
			chkRS.setErrorMsg(MessageFormat.format(Messages.ENABLED_NOT_DELETE, "软件镜像"));
			return chkRS;
		}

		// 软件镜像关联资源检查
		if (subOrderDao.countBySubServSoftwareImageId(subServSoftwareImage.getId()) > 0) {
			chkRS.setStatus(HttpStatus.SC_CONFLICT);
			chkRS.setErrorMsg("该软件镜像已关联订单，无法删除");
			return chkRS;
		}

		return chkRS;
	}

	/**
	 * 新增时非业务逻辑(非空和长度)检查
	 * 
	 * @param paramMap
	 *            请求参数
	 * @return CheckResult检查结果对象
	 */
	private CheckResult checkSaveNonLogic(Map<String, Object> paramMap) {
		CheckResult chkRS = new CheckResult();
		// 站点非空检查
		String siteId = (String) paramMap.get("site");
		if (StringUtils.isBlank(siteId)) {
			chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
			chkRS.setErrorMsg(MessageFormat.format(Messages.NULL_NOT_ALLOWED, "所属站点"));
			return chkRS;
		}

		// 软件主次版本非空检查
		String dfSubServSoftwareId = (String) paramMap.get("definitionSubServSoftware");
		if (StringUtils.isBlank(dfSubServSoftwareId)) {
			chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
			chkRS.setErrorMsg(MessageFormat.format(Messages.NULL_NOT_ALLOWED, "软件主次版本"));
			return chkRS;
		}

		// 修订版本非空和有效性检查
		Integer patchVersion = (Integer) paramMap.get("patchVersion");
		if (patchVersion == null) {
			chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
			chkRS.setErrorMsg(MessageFormat.format(Messages.NULL_NOT_ALLOWED, "修订版本"));
			return chkRS;
		}
		if (patchVersion > 99 || patchVersion < 0) {
			chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
			chkRS.setErrorMsg(MessageFormat.format(Messages.BETWEEN_MIN_AND_MAX, "修订版本", 0, 99));
			return chkRS;
		}

		// 编译版本非空和有效性检查
		Integer buildVersion = (Integer) paramMap.get("buildVersion");
		if (buildVersion == null) {
			chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
			chkRS.setErrorMsg(MessageFormat.format(Messages.NULL_NOT_ALLOWED, "编译版本"));
			return chkRS;
		}
		if (buildVersion > 99 || buildVersion < 0) {
			chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
			chkRS.setErrorMsg(MessageFormat.format(Messages.BETWEEN_MIN_AND_MAX, "编译版本", 0, 99));
			return chkRS;
		}

		if (paramMap.containsKey("description")) {
			// 描述长度检查
			String description = (String) paramMap.get("description");
			if (StringUtils.trim(description).length() > 256) {
				chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
				chkRS.setErrorMsg(MessageFormat.format(Messages.MAXLENGTH_NOT_ALLOWED, "描述", 256));
				return chkRS;
			}
		}
		return chkRS;
	}

	/**
	 * 新增时业务逻辑检查
	 * 
	 * @param paramMap
	 *            请求参数
	 * @return CheckResult检查结果对象
	 */
	private CheckResult checkSaveLogic(Map<String, Object> paramMap) {
		CheckResult chkRS = new CheckResult();

		// 站点存在性检查
		String siteId = (String) paramMap.get("site");
		SiteEntity site = siteDao.get(siteId);
		if (site == null) {
			chkRS.setStatus(HttpStatus.SC_GONE);
			chkRS.setErrorMsg(MessageFormat.format(Messages.NOT_EXIST, "所属站点"));
			return chkRS;
		}

		// 主次版本存在性检查
		String dfSubServSoftwareId = (String) paramMap.get("definitionSubServSoftware");
		DefinitionSubServSoftwareEntity dfSubServSoftware = definitionSubServSoftwareDao.get(dfSubServSoftwareId);
		if (dfSubServSoftware == null) {
			chkRS.setStatus(HttpStatus.SC_GONE);
			chkRS.setErrorMsg(MessageFormat.format(Messages.NOT_EXIST, "主次版本"));
			return chkRS;
		}

		// 软件版本重复性检查
		Integer patchVersion = (Integer) paramMap.get("patchVersion");
		Integer buildVersion = (Integer) paramMap.get("buildVersion");
		int imageCount = subServSoftwareImageDao.countImage(siteId, dfSubServSoftwareId, patchVersion, buildVersion);
		if (imageCount != 0) {
			chkRS.setStatus(HttpStatus.SC_CONFLICT);
			chkRS.setErrorMsg(MessageFormat.format(Messages.EXIST, "软件版本"));
			return chkRS;
		}
		return chkRS;
	}
}
